package chen.web.back.client.system;

import java.util.LinkedHashMap;
import java.util.Map;

import chen.web.back.client.Entry;
import chen.web.back.client.EntrySelectionListener;
import chen.web.back.client.Workbench;

import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Widget;

public class SystemConfigNavigation extends ContentPanel implements ValueChangeHandler<String> {
	
	private static Map<Entry, Class<? extends Widget>> mapping = new LinkedHashMap<Entry, Class<? extends Widget>>();
	
	static {
		mapping.put(new Entry("站点设置", "site"), SiteForm.class);
		mapping.put(new Entry("邮箱设置", "mailbox"), MailboxForm.class);
		mapping.put(new Entry("缓存管理", "cache"), CachePanel.class);
		mapping.put(new Entry("log4j配置", "log4j"), Log4jForm.class);
		mapping.put(new Entry("防火墙", "firewall"), FirewallForm.class);
	}
	
	public SystemConfigNavigation() {
		this.setHeading("系统设置");
		this.getHeader().setStyleAttribute("font-size", "13px");
		this.setBodyBorder(false);

		ListView<Entry> list = new ListView<Entry>();
		ListStore<Entry> store = new ListStore<Entry>();
		for(Entry entry: mapping.keySet()){
			store.add(entry);
		}
		
		list.setStore(store);
		list.setDisplayProperty("name");
		list.setBorders(false);
		list.setStyleAttribute("color", "#416AA3");
		list.setStyleAttribute("text-indent", "1em");
		list.getSelectionModel().addSelectionChangedListener(new EntrySelectionListener());
		
		this.add(list);
		History.addValueChangeHandler(this);
		History.fireCurrentHistoryState();
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		// TODO Auto-generated method stub
		String url = event.getValue();
		for(Map.Entry<Entry, Class<? extends Widget>> mapEntry : mapping.entrySet()){
			if(mapEntry.getKey().getURL().equals(url)){
				TabItem tab = Workbench.getInstance().generalTabItem(mapEntry.getKey());
				tab.add((Widget) GWT.create(mapEntry.getValue()));
				Workbench.getInstance().addTabItem(tab);
			}			
		}
	}

/*	
	private List<ModelData> getData(){
		List<ModelData> list = new ArrayList<ModelData>();
		for(int i=0; i<3; i++){
			ModelData data = new BaseModelData();
			list.add(data);
		}
		list.get(0).set("name", "系统配置");
		list.get(1).set("name", "邮箱配置");
		list.get(2).set("name", "系统配置");
		return list;
	}*/

}
